import Message from '../../../miniprogram_npm/tdesign-miniprogram/message/index';

Page({
  data: {
    otr: null,
    mode: '',
    dateVisible: false,
    showWarnConfirm: false,
    date: new Date().getTime(),
    registrationDate: '',

    // 指定选择区间起始值
    start: '2012-03-15 00:00:00',
    end: '2025-03-15 00:00:00',
    filter(type, options) {
      if (type === 'year') {
        return options.sort((a, b) => b.value - a.value);
      }
      return options;
    },
    popupProps: {
      usingCustomNavbar: true,
    },
    priceError: false,
    priceFormat: (v) => {
      const isNumber = /^\d+(\.\d+)?$/.test(v);
      if (isNumber) {
        return parseFloat(v).toFixed(2);
      }
      return v;
    },
  },
  onLoad(options) {
      // 获取当前日期
      const now = new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, '0');
      const day = String(now.getDate()).padStart(2, '0');
      const hours = String(now.getHours()).padStart(2, '0');
      const minutes = String(now.getMinutes()).padStart(2, '0');
      const seconds = String(now.getSeconds()).padStart(2, '0');

      // 计算13年前的日期
      const thirteenYearsAgo = new Date(now);
      thirteenYearsAgo.setFullYear(thirteenYearsAgo.getFullYear() - 13);
      const thirteenYearsAgoYear = thirteenYearsAgo.getFullYear();
      const thirteenYearsAgoMonth = String(thirteenYearsAgo.getMonth() + 1).padStart(2, '0');
      const thirteenYearsAgoDay = String(thirteenYearsAgo.getDate()).padStart(2, '0');
      const thirteenYearsAgoHours = String(thirteenYearsAgo.getHours()).padStart(2, '0');
      const thirteenYearsAgoMinutes = String(thirteenYearsAgo.getMinutes()).padStart(2, '0');
      const thirteenYearsAgoSeconds = String(thirteenYearsAgo.getSeconds()).padStart(2, '0');

      // 格式化日期字符串
      const startDate = `${thirteenYearsAgoYear}-${thirteenYearsAgoMonth}-${thirteenYearsAgoDay} ${thirteenYearsAgoHours}:${thirteenYearsAgoMinutes}:${thirteenYearsAgoSeconds}`;
      const endDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      const { otr, motorcycleName } = options;
      // 更新页面数据
      this.setData({
          start: startDate,
          end: endDate,
          otr,
          motorcycleName
      });
  },
  onPriceInput(e) {
    const { priceError } = this.data;
    const isNumber = /^\d+(\.\d+)?$/.test(e.detail.value);
    if (priceError === isNumber) {
      this.setData({
        priceError: !isNumber,
      });
    }
  },
  showPicker() {
    this.setData({
      dateVisible: true,
    });
  },
  onConfirm(e) {
    const { value } = e.detail;
    this.setData({
      registrationDate: value
    });
  },
  showWarning(content) {
    Message.warning({
      context: this,
      offset: [10, 32],
      duration: 2000,
      content,
    });
  },
  handleCheckDepreciationClick() {
    const {otr, registrationDate} = this.data
    if (otr == null || otr == undefined || otr == 0) {
      this.showWarning('落地价不能为空！')
      return
    } else if (registrationDate == '') {
      this.showWarning('注册登记日期不能为空！')
      return
    } else {    
      wx.navigateTo({
        url: `../../CheckDepreciationPage/index/index?otr=${otr}&scrapStartDate=${registrationDate}&motorcycleName=${this.data.motorcycleName}`
      })
    }
  }
});
